#  SUSAN® - Sole of Unix Save ANything
#
# Copyright (C) 2019-2020 Hechzh Property
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

cmake_minimum_required(VERSION 3.0)

project(susan-docs NONE)

# commands building json files ######
add_custom_command(
  OUTPUT
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-dir-config-schema.json
  COMMAND susan-dir -xs >/dev/null
  COMMAND
    susan-dir -xs
    >${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-dir-config-schema.json
  COMMAND
    sed --in-place --expression="/\\\"version\\\":/d"
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-dir-config-schema.json
  DEPENDS $<$<BOOL:${docs-build-json}>:susan-dir>
)

add_custom_command(
  OUTPUT
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-sd-config-schema.json
  COMMAND susan-sd -xs >/dev/null
  COMMAND
    susan-sd -xs
    >${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-sd-config-schema.json
  COMMAND
    sed --in-place --expression="/\\\"version\\\":/d"
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-sd-config-schema.json
  DEPENDS $<$<BOOL:${docs-build-json}>:susan-sd>
)

add_custom_command(
  OUTPUT
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-fd-config-schema.json
  COMMAND susan-fd -xs >/dev/null
  COMMAND
    susan-fd -xs
    >${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-fd-config-schema.json
  COMMAND
    sed --in-place --expression="/\\\"version\\\":/d"
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-fd-config-schema.json
  DEPENDS $<$<BOOL:${docs-build-json}>:susan-fd>
)

add_custom_command(
  OUTPUT
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
  COMMAND bconsole -xs >/dev/null
  COMMAND
    bconsole -xs
    >${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
  COMMAND
    sed --in-place --expression="/\\\"version\\\":/d"
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
  DEPENDS $<$<BOOL:${docs-build-json}>:bconsole>
)

add_custom_command(
  OUTPUT
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-tray-monitor-config-schema.json
  COMMAND susan-tray-monitor -xs >/dev/null
  COMMAND
    susan-tray-monitor -xs
    >${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-tray-monitor-config-schema.json
  COMMAND
    sed --in-place --expression="/\\\"version\\\":/d"
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-tray-monitor-config-schema.json
  DEPENDS $<$<BOOL:${docs-build-json}>:susan-tray-monitor>
)

set(AUTOGENERATED_RESOURCE_DESCRIPTION_JSON_FILES
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-dir-config-schema.json
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-sd-config-schema.json
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-fd-config-schema.json
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
    ${PROJECT_SOURCE_DIR}/source/include/autogenerated/susan-tray-monitor-config-schema.json
)

if(${docs-build-json})
  set(CLEAN_JSON_FILES
      rm -f ${PROJECT_SOURCE_DIR}/source/include/autogenerated/*.json
  )
endif()

# target: clean-docs ######
add_custom_target(
  clean-docs
  COMMAND ${CLEAN_JSON_FILES}
  COMMAND rm -f ${PROJECT_SOURCE_DIR}/source/include/autogenerated/*.inc
  COMMAND rm -rf ${PROJECT_BINARY_DIR}/SusanMainReference
)

# target: check-git ###### To prevent documentation to be built out of sync with
# the actual binaries, this target checks if the generated json files differ
# from the ones in the repository.

add_custom_target(
  check-git
  COMMAND git diff -p --exit-code --stat=80
          ${PROJECT_SOURCE_DIR}/source/include/autogenerated
  DEPENDS ${AUTOGENERATED_RESOURCE_DESCRIPTION_JSON_FILES}
  WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  COMMENT
    "Comparing json files in \"${PROJECT_SOURCE_DIR}/source/include/autogenerated\" with git repo"
)

set(SPHINX_OPTS $ENV{SPHINX_OPTS})
set(SPHINX_COMMAND sphinx-build)
set(SPHINX_BUILDDIR ${PROJECT_BINARY_DIR}/SusanMainReference)
set(SPHINX_SOURCE_DIR ${PROJECT_SOURCE_DIR}/source)

if(${docs-only})
  set(DOCS_ALL ALL) # this enables "make docs" by just typing "make"
endif()

# target: docs ######
add_custom_target(
  docs
  ${DOCS_ALL}
  COMMAND ${SPHINX_COMMAND} -M html "${SPHINX_SOURCE_DIR}" "${SPHINX_BUILDDIR}"
          "${SPHINX_OPTS}"
  COMMAND ${SPHINX_COMMAND} -b man "${SPHINX_SOURCE_DIR}" "${SPHINX_BUILDDIR}"
          "${SPHINX_OPTS}"
  DEPENDS $<$<BOOL:${docs-build-json}>:clean-docs>
  DEPENDS check-git
)
